package acs.bridge.jreport;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import org.apache.log4j.Logger;

import java.io.ByteArrayOutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

/**
 * acs.jreport.JReportExportExcel
 * User: KuznetsovStV
 * Date: 24.12.11
 * Time: 11:15
 * Class for producing excel report
 */
public class JReportExportExcel implements JReportExportInterface {
    Logger logger = Logger.getLogger(this.getClass().getName());
    final String cExtension = ".xls";
    private Properties exportExcelparam;


    public String getExtension() {
        return cExtension;
    }

    public byte[] exportReport(JasperPrint jasperPrint) throws JRException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        JRXlsExporter exporterXLS = new JRXlsExporter();

        logger.info("Exporting to excel ...");

        exporterXLS.setParameter(JRXlsExporterParameter.IS_FONT_SIZE_FIX_ENABLED,
                exportExcelparam==null?true:exportExcelparam.getProperty("IS_FONT_SIZE_FIX_ENABLED","true").equals("true")?true:false
              );
        exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
                exportExcelparam==null?false:exportExcelparam.getProperty("IS_ONE_PAGE_PER_SHEET","false").equals("true")?true:false

               );

        exporterXLS.setParameter(JRXlsExporterParameter.MAXIMUM_ROWS_PER_SHEET,
                exportExcelparam==null?new Integer(65000):Integer.getInteger(exportExcelparam.getProperty("MAXIMUM_ROWS_PER_SHEET","65000"))
               );

        exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS,
                exportExcelparam==null?true:exportExcelparam.getProperty("IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS","true").equals("true")?true:false
               );

        exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
                exportExcelparam==null?true:exportExcelparam.getProperty("IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS","true").equals("true")?true:false
               );

        exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE,
                exportExcelparam==null?false:exportExcelparam.getProperty("IS_DETECT_CELL_TYPE","false").equals("true")?true:false);

        /* Входной поток для экспорта в xls*/
        /* exporterXLS.setParameter(JRXlsExporterParameter.INPUT_STREAM,
       inputStream); */

        exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT,
                jasperPrint);

        /* Выходной поток для экспорта в xls*/
        exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,
                outputStream);


        exporterXLS.exportReport();

        //l_OutputStream.reset();
        //l_OutputStream.

        return outputStream.toByteArray();

    }


    public void setExportExcelparam(Properties exportExcelparam) {
        this.exportExcelparam = exportExcelparam;
    }

    public Properties getExportExcelparam() {
        return exportExcelparam;
    }
}
